package com.example.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.example.annotation.rest.AnonymousGetMapping;
import com.example.annotation.rest.AnonymousPostMapping;
import com.example.excel.listener.CourseTableDataListener;
import com.example.excel.listener.JobTableDataListener;
import com.example.pojo.*;
import com.example.service.CourseResultService;
import com.example.service.CourseTableService;
import com.example.service.JobTableService;
import com.example.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述
 *
 * @Author xulitao
 * @Date 2022/3/7
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/edu")
@CrossOrigin
@Api("教育模块")
public class EduPlatController {

    @Autowired
    private CourseResultService courseResultService;
    @Resource
    private JobTableService jobTableService;
    @Resource
    private CourseTableService courseTableService;

    @AnonymousPostMapping("/uploadCourseTable")
    @ApiOperation("考试成绩excel导入")
    public ResponseEntity uploadCourseTable(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), CourseTableEntity.class, new CourseTableDataListener(courseTableService))
                    .sheet().doRead();
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
        return ResponseEntity.ok().build();
    }

    @AnonymousPostMapping("/uploadJobTable")
    @ApiOperation("作业成绩excel导入")
    public ResponseEntity uploadJobTable(@RequestParam("file") MultipartFile file) {
        UserDetails currentUser = SecurityUtils.getCurrentUser();
        try {
            EasyExcel.read(file.getInputStream(), new JobTableDataListener(jobTableService))
                    .extraRead(CellExtraTypeEnum.MERGE).sheet().doRead();
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
        return ResponseEntity.ok().build();
    }

    @AnonymousPostMapping("/queryStuScore")
    @ApiOperation("查询学生成绩")
    public Object queryStuScore(@RequestBody StuScoreVo vo) {
        PageVo pageVo = courseResultService.queryList(vo);
        return pageVo;
    }

    @AnonymousPostMapping("/getAllStu")
    @ApiOperation("查询全部学生")
    public Object getAllStu() {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("data",courseResultService.queryStu());
        paramMap.put("code",200);
        paramMap.put("mgs","success");
        return paramMap;
    }

    @AnonymousPostMapping("/insertStuScore")
    @ApiOperation("录入学生成绩")
    public Object insertStuScore(@RequestBody List<CourseResultEntity> list) {
        return courseResultService.insertStuScore(list);
    }

    @AnonymousPostMapping("/getAllSubject")
    @ApiOperation("获取全部学科")
    public ResultVo getAllSubject() {
        return courseResultService.getAllSubject();
    }

    @AnonymousPostMapping("/updateStuScore")
    @ApiOperation("更新学生成绩")
    public ResultVo updateStuScore(@RequestBody CourseResultEntity entity) {
        return courseResultService.updateStuScore(entity);
    }

    @AnonymousPostMapping("/delStuScore")
    @ApiOperation("删除学生成绩")
    public ResultVo delStuScore(@RequestBody CourseResultEntity entity) {
        return courseResultService.delStuScore(entity);
    }

    @AnonymousPostMapping("/insertWorkFinish")
    @ApiOperation("进行作业布置")
    public ResultVo insertWorkFinish(@RequestBody List<WordFinishVo> list) {
        return courseResultService.insertWorkFinish(list);
    }

    @AnonymousPostMapping("/updateWorkFinish")
    @ApiOperation("更新布置作业")
    public ResultVo updateWorkFinish(@RequestBody WordFinishVo entity) {
        return courseResultService.updateWorkFinish(entity);
    }

    @AnonymousPostMapping("/delWorkFinish")
    @ApiOperation("删除作业布置")
    public ResultVo delWorkFinish(@RequestBody WordFinishVo entity) {
        return courseResultService.delWorkFinish(entity);
    }


    @AnonymousGetMapping("/insertPlayStation")
    @ApiOperation("互动情况入库")
    public ResultVo insertPlayStation( AskAnswerVo vo) {
        if(ObjectUtil.isEmpty(vo)){
            throw new RuntimeException("参数不能为空！");
        }
        return courseResultService.insertPlayStation(vo);
    }

    @AnonymousGetMapping("/delPlayStation")
    @ApiOperation("删除互动情况")
    public ResultVo delPlayStation(@RequestParam(value = "idList") String idList) {
        System.out.println(idList);
       return courseResultService.delPlayStation(Arrays.asList(idList.split(",")));
    }

    @AnonymousGetMapping("/getPlayStation")
    @ApiOperation("查询互动情况")
    public PageVo<AskAnswerEntity> getPlayStation(@RequestParam Integer pageIndex,
                                                  @RequestParam Integer pageSize,
                                                  @RequestParam(value = "stuNumber",required = false) String stuNumber,
                                                  @RequestParam(value = "stuName",required = false) String stuName) {
        AskAnswerEntity askAnswerEntity= new AskAnswerEntity();
        askAnswerEntity.setPageIndex(pageIndex);
        askAnswerEntity.setPageSize(pageSize);
        askAnswerEntity.setStuName(stuName);
        askAnswerEntity.setStuNumber(stuNumber);
        return courseResultService.getPlayStation(askAnswerEntity);
    }

    @AnonymousPostMapping("/getWorkFinish")
    @ApiOperation("查询作业布置情况")
    public PageVo<WorkFinishEntity> getWorkFinish(@RequestBody WordFinishVo entity) {
        return courseResultService.getWorkFinish(entity);
    }

    @AnonymousGetMapping("/getCourseTable")
    @ApiOperation("成绩管理列表查询")
    public PageVo<CourseTableEntity> getCourseTable(@RequestParam Integer pageIndex,
                                                    @RequestParam Integer pageSize,
                                                    @RequestParam(value = "stuNumber",required = false) String stuNumber,
                                                    @RequestParam(value = "stuName",required = false) String stuName) {
        CourseTableEntity courseTableEntity = new CourseTableEntity();
        courseTableEntity.setPageIndex(pageIndex);
        courseTableEntity.setPageSize(pageSize);
        courseTableEntity.setStuName(stuName);
        courseTableEntity.setStuNumber(stuNumber);
        return courseResultService.getCourseTable(courseTableEntity);
    }

    @AnonymousGetMapping("/delBatchCourse")
    @ApiOperation("成绩管理列表删除")
    public ResultVo delBatchCourse(@RequestParam(value = "idList") String idList) {
        System.out.println(idList);
        return courseResultService.delCourseTable(Arrays.asList(idList.split(",")));
    }

    @AnonymousGetMapping("/delAllCourse")
    @ApiOperation("成绩管理列表删除全部")
    public ResultVo delAllCourse() {
        Long currentUserId = SecurityUtils.getCurrentUserId();
        return courseResultService.delAllCourseTable(currentUserId);
    }


    @AnonymousGetMapping("/getJobTable")
    @ApiOperation("作业管理列表查询")
    public PageVo<JobTableEntity> getJobTable(@RequestParam Integer pageIndex,
                                              @RequestParam Integer pageSize,
                                              @RequestParam(value = "stuNumber", required = false) String stuNumber,
                                              @RequestParam(value = "stuName", required = false) String stuName
    ) {

        JobTableEntity jobTableEntity = new JobTableEntity();
        jobTableEntity.setPageIndex(pageIndex);
        jobTableEntity.setPageSize(pageSize);
        jobTableEntity.setStuName(stuName);
        jobTableEntity.setStuNumber(stuNumber);
        return courseResultService.getJobTable(jobTableEntity);
    }

    @AnonymousGetMapping("/delBatchJob")
    @ApiOperation("作业管理列表删除")
    public ResultVo delBatchJob(@RequestParam(value = "idList") String idList) {
        System.out.println(idList);
        if (ObjectUtil.isEmpty(idList)) {
            return null;
        }
        return courseResultService.delJobTable(Arrays.asList(idList.split(",")));
    }

    @AnonymousGetMapping("/delAllJob")
    @ApiOperation("作业管理列表清空")
    public ResultVo delAllJob() {
        Long currentUserId = SecurityUtils.getCurrentUserId();
        return courseResultService.delAllJobTable(currentUserId);
    }
}
